package com.example.musiccommunity.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ImageUtil {
    public static byte[] bitmabToBytes(Drawable b){
        //将图片转化为位图
        BitmapDrawable bd = (BitmapDrawable)b;
        Bitmap bitmap = bd.getBitmap();
        //Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), bitmapt);
        int size = bitmap.getWidth() * bitmap.getHeight() * 4;
        //创建一个字节数组输出流,流的大小为size
        ByteArrayOutputStream baos= new ByteArrayOutputStream(size);
        try {
            //设置位图的压缩格式，质量为100%，并放入字节数组输出流中
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            //将字节数组输出流转化为字节数组byte[]
            byte[] imageData = baos.toByteArray();
            return imageData;
        }catch (Exception e){
        }finally {
            try {
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new byte[0];


    }
//    public static Bitmap compressImage1(Bitmap image) {
//
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        image.compress(Bitmap.CompressFormat.JPEG, 30, baos);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
//        int options = 100;
//        while (baos.toByteArray().length / 1024 > 100) { //循环判断如果压缩后图片是否大于300kb,大于继续压缩
//            baos.reset();//重置baos即清空baos
//            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
//            options -= 10;//每次都减少10
//        }
//        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
//        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
//        return bitmap;
//    }
    public static Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 20, baos); // 初始压缩（100表示不压缩）
        System.out.println("大小："+baos.toByteArray().length / 1024 );
        int options =20; //
        while (baos.toByteArray().length / 1024 > 60 && options>1 ) { // 添加options >= 10的条件
            baos.reset();
            options -= 1;
            if (options < 1) {
                options=1;
            }
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);
            System.out.println("大小："+baos.toByteArray().length / 1024 );
        }

        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        Bitmap b=BitmapFactory.decodeStream(isBm, null, null);
        int newWidth = b.getWidth() / 2; // Example: half the width
        int newHeight = b.getHeight() / 2; // Example: half the height
        return Bitmap.createScaledBitmap(b, newWidth, newHeight, true);
    }
    public static byte[] getBytesByBitmap(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bitmap.getByteCount());
        bitmap.compress(Bitmap.CompressFormat.PNG, 10, outputStream);

        return outputStream.toByteArray();
    }

    // 可选：将 Base64 转换为 Bitmap 以便在 ImageView 中显示
    public static Bitmap base64ToBitmap(String base64String) {
        try {
            byte[] decodedBytes = Base64.decode(base64String, Base64.DEFAULT);
            return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



}
